// PrPagePlugins.cpp : implementation file
//

#include "stdafx.h"
#include "ansieditor.h"
#include "PrPagePlugins.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPrPagePlugins dialog


CPrPagePlugins::CPrPagePlugins(CWnd* pParent /*=NULL*/)
	: CPrPage(CPrPagePlugins::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPrPagePlugins)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_bValidated = FALSE;
}


void CPrPagePlugins::DoDataExchange(CDataExchange* pDX)
{
	CPrPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPrPagePlugins)
	DDX_Control(pDX, IDC_BTN_ABOUT, m_btnAbout);
	DDX_Control(pDX, IDC_BTN_CONFIG, m_btnConfig);
	DDX_Control(pDX, IDC_LIST_PLUGINS, m_listPlugins);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPrPagePlugins, CPrPage)
	//{{AFX_MSG_MAP(CPrPagePlugins)
	ON_BN_CLICKED(IDC_BTN_ABOUT, OnBtnAbout)
	ON_LBN_SELCHANGE(IDC_LIST_PLUGINS, OnSelchangeListPlugins)
	ON_BN_CLICKED(IDC_BTN_CONFIG, OnBtnConfig)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPrPagePlugins message handlers

BOOL CPrPagePlugins::OnInitDialog() 
{
	CPrPage::OnInitDialog();
	
	CPlugin *pPlugin = NULL;
	int nCount = gpPluginManager->GetPluginCount();
	for(int n = 0; n < nCount; n++)
	{
		pPlugin = gpPluginManager->GetPluginByIndex(n);
		int nIndex = m_listPlugins.AddString(pPlugin->GetName());
		m_listPlugins.SetItemData(nIndex, (DWORD)pPlugin);
		m_listPlugins.SetCheck(nIndex, pPlugin->IsEnabled());
	}
	if(nCount)
	{
		m_listPlugins.SetCurSel(0);
	}
	OnSelchangeListPlugins();
	return TRUE;
}

void CPrPagePlugins::OnBtnAbout() 
{
	GetSelectedPlugin()->ShowAbout();
}

CPlugin* CPrPagePlugins::GetSelectedPlugin()
{
	int nIndex = m_listPlugins.GetCurSel();
	if(nIndex == -1)
	{
		return NULL;
	}
	return (CPlugin*)m_listPlugins.GetItemData(nIndex);
}

void CPrPagePlugins::OnSelchangeListPlugins() 
{
	CPlugin *pPlugin = GetSelectedPlugin();
	BOOL bSupportsConfig = pPlugin->SupportsConfig();
	BOOL bSupportsAbout = pPlugin->SupportsAbout();
	m_btnConfig.EnableWindow(bSupportsConfig);
	m_btnAbout.EnableWindow(bSupportsAbout);
	SetDlgItemText(IDC_LBL_FILENAME, pPlugin->GetFileName());
	SetDlgItemText(IDC_TXT_DESCRIPTION, pPlugin->GetDescription());
}

void CPrPagePlugins::OnBtnConfig() 
{
	GetSelectedPlugin()->BeginConfig();
}

BOOL CPrPagePlugins::ValidateData()
{
	int nCount = m_listPlugins.GetCount();
	CString sDisabledPlugins;
	for(int n = 0; n < nCount; n++)
	{
		CPlugin *pPlugin = (CPlugin*)m_listPlugins.GetItemData(n);
		if(pPlugin->SupportsConfig())
		{
			pPlugin->SaveConfig();
		}
		BOOL bEnabled = m_listPlugins.GetCheck(n);
		if(!bEnabled)
		{
			CString sTemp;
			sTemp.Format("[%s]", pPlugin->GetFileName());
			sDisabledPlugins += sTemp;
		}
		pPlugin->Enable(bEnabled);
	}
	gpApp->m_options.sDisabledPlugins = sDisabledPlugins;

	return CPrPage::ValidateData();
}

void CPrPagePlugins::OnDestroy() 
{
	if(!m_bValidated)
	{
		int nCount = m_listPlugins.GetCount();
		for(int n = 0; n < nCount; n++)
		{
			CPlugin *pPlugin = (CPlugin*)m_listPlugins.GetItemData(n);
			if(pPlugin->SupportsConfig())
			{
				pPlugin->CancelConfig();
			}
		}
	}
	CPrPage::OnDestroy();	
}